import org.joda.time.*
import org.joda.time.format.*

// get the Operating Day as today
    def DateTimeZone timezone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("Europe/London"))

    def now = new DateTime(timezone)

/////////////////////////////////////////////////////////////////
// read the business process timeline
    Logger logger = new Logger()

    reader = new OrgReader("ERCOT")
    // set the logger
    reader.setLogger(logger)

    reader.readBPTimeline("./inputdata/orgbptimeline.csv")
    println "Finished reading:"+ reader.org.name

// create the event Pts
    def eventPtsCreator = new EventPtsCreator(reader.org)

    def bpEventPts = eventPtsCreator.getAllBPEventPts(now)
    println "BPEventPts.size:" + bpEventPts.size()

    def utils = new Utils()

    println "Operating Date:"+ utils.getFormattedDateTime(now)


// now creating the Business process event Pts
    def timelineXml = "<data>"
    bpEventPts.each {
        timelineXml = timelineXml << it.getXml()
    }

    timelineXml << "</data>"

//println timelineXml

// now writing the Business process event Pts
    println "writing BP event Pts to Xml file"

    def file = new File("timelinedata/bptimeline.xml")

    file.write(timelineXml.toString())

    println "finished writing BP eventPts to Xml file"
    
/////////////////////////////////////////////////////////////////
// now creating the Sub process event Pts
    reader.readSubProcessTimeline("./inputdata/subprocesstimeline.csv")


    def spEventPts = eventPtsCreator.getAllSPEventPts(now)
    println "SPEventPts.size:" + spEventPts.size()


    timelineXml = "<data>"
    spEventPts.each {
        timelineXml = timelineXml << it.getXml()
    }

    timelineXml << "</data>"

    //println timelineXml


// now writing the Sub process event Pts
    println "writing SP event Pts to Xml file"

    file = new File("timelinedata/sptimeline.xml")

    file.write(timelineXml.toString())

    println "finished writing SP eventPts to Xml file"

/////////////////////////////////////////////////////////////////
// now creating the App Service event Pts
    reader.readAppServiceTimeline("./inputdata/appservicestimeline.csv")






















/*
// USING JDK GREGORIAN CALENDAR
// initialize the date
def c= new GregorianCalendar()
c.set( Calendar.ERA, GregorianCalendar.AD )
c.set( Calendar.YEAR, 2009 )
c.set( Calendar.MONTH, 3 )
c.set( Calendar.DATE,  8)
def startDateStr = String.format('%tA %<te %<tB %<ty', c)

c.add(Calendar.DATE, +7)
//c.add(Calendar.DATE, -1)

def endDateStr = String.format('%tA %<te %<tB %<ty', c)
def timelineData = bpEventPtsCreator.getDummyTimelineXml(startDateStr, endDateStr, "GMT")

def file2 = new File("1sttimeline.xml")
file2.write(timelineData)
*/

/*
org.orgServices.each { key, myOrgService -> 
    println  "\t|----OS:" + myOrgService.name

    myOrgService.businessFunctions.each { bfkey, myBusinessFunction ->
        println "\t\t|----BF:" + myBusinessFunction.name

        myBusinessFunction.businessProcesses.each { bpkey, myBusinessProcess ->
            println "\t\t\t|----BP:" + myBusinessProcess.name
            println "\t\t\t\t|-----timeline:" + myBusinessProcess.timelineItem
        }
    }
}
*/
